package com.drought.rained.business;

import android.app.Activity;
import android.text.TextUtils;

import com.alibaba.android.arouter.launcher.ARouter;
import com.drought.rained.common.ConstantLJ;
import com.google.gson.Gson;
import com.drought.rained.LJApplication;
import com.drought.rained.bean.AuthDataLJ;
import com.drought.rained.cache.AcacheLJ;
import com.drought.rained.cache.AcacheKeysLJ;
import com.drought.rained.common.AuthCodeLJ;

import java.util.Arrays;
import java.util.List;

public class AuthRouterLJ {
    private static AuthRouterLJ instance;
    private String currentStep = AuthCodeLJ.IDENTITY_CODE;

    public List<String> getRouterList() {
        String authData = AcacheLJ.get(LJApplication.getContext()).getAsString(AcacheKeysLJ.AUTH);
        if (TextUtils.isEmpty(authData)) return null;
        Gson gson = new Gson();
        AuthDataLJ data = gson.fromJson(authData, AuthDataLJ.class);
        if (data.getAllCodes().size() < 7) {
            String[] arr = {AuthCodeLJ.IDENTITY_CODE, AuthCodeLJ.COMPANY_CODE, AuthCodeLJ.BORROWING_NEED_CODE,
                    AuthCodeLJ.PERSONAGE_INFO_CODE, AuthCodeLJ.CONTACTS_CODE, AuthCodeLJ.BANK_INFO_CODE,
                    AuthCodeLJ.ADDITIONAL_INFORMATION_CODE};
            return Arrays.asList(arr);
        }
        return data.getAllCodes();
    }

    public List<String> getNoAuthRouterList() {
        String authData = AcacheLJ.get(LJApplication.getContext()).getAsString(AcacheKeysLJ.AUTH);
        if (TextUtils.isEmpty(authData)) return null;
        Gson gson = new Gson();
        AuthDataLJ data = gson.fromJson(authData, AuthDataLJ.class);
        if (data.getAllCodes().size() < 7) {
            String[] arr = {AuthCodeLJ.IDENTITY_CODE, AuthCodeLJ.COMPANY_CODE, AuthCodeLJ.BORROWING_NEED_CODE,
                    AuthCodeLJ.PERSONAGE_INFO_CODE, AuthCodeLJ.CONTACTS_CODE, AuthCodeLJ.BANK_INFO_CODE,
                    AuthCodeLJ.ADDITIONAL_INFORMATION_CODE};
            return Arrays.asList(arr);
        }
        return data.getNotCodes();
    }


    public static AuthRouterLJ getInstance() {
        if (instance == null) {
            instance = new AuthRouterLJ();
        }

        return instance;
    }


    public void jumpPage(String page) {
        switch (page) {
            case AuthCodeLJ.IDENTITY_CODE:
                ARouter.getInstance().build(RouterMapLJ.REAL_NAME_AUTH_PAGE).navigation();
                break;
            case AuthCodeLJ.COMPANY_CODE:
                ARouter.getInstance().build(RouterMapLJ.WORK_AUTH_PAGE).navigation();
                break;
            case AuthCodeLJ.BORROWING_NEED_CODE:
                ARouter.getInstance().build(RouterMapLJ.MONEY_AUTH_PAGE).navigation();
                break;
            case AuthCodeLJ.PERSONAGE_INFO_CODE:
                ARouter.getInstance().build(RouterMapLJ.PERSONAL_AUTH_PAGE).navigation();
                break;
            case AuthCodeLJ.CONTACTS_CODE:
                ARouter.getInstance().build(RouterMapLJ.CONTACT_AUTH_PAGE).navigation();
                break;
            case AuthCodeLJ.BANK_INFO_CODE:
                ARouter.getInstance().build(RouterMapLJ.BANK_AUTH_PAGE).navigation();
                break;
            case AuthCodeLJ.ADDITIONAL_INFORMATION_CODE:
                ARouter.getInstance().build(RouterMapLJ.PHOTO_AUTH_PAGE).navigation();
                break;
            default:
                break;
        }
    }

    public void jumpPageAndFinish(Activity activity, String page) {
        switch (page) {
            case AuthCodeLJ.IDENTITY_CODE:
                ARouter.getInstance().build(RouterMapLJ.REAL_NAME_AUTH_PAGE).navigation();
                activity.finish();
                break;
            case AuthCodeLJ.COMPANY_CODE:
                ARouter.getInstance().build(RouterMapLJ.WORK_AUTH_PAGE).navigation();
                activity.finish();
                break;
            case AuthCodeLJ.BORROWING_NEED_CODE:
                ARouter.getInstance().build(RouterMapLJ.MONEY_AUTH_PAGE).navigation();
                activity.finish();
                break;
            case AuthCodeLJ.PERSONAGE_INFO_CODE:
                ARouter.getInstance().build(RouterMapLJ.PERSONAL_AUTH_PAGE).navigation();
                activity.finish();
                break;
            case AuthCodeLJ.CONTACTS_CODE:
                ARouter.getInstance().build(RouterMapLJ.CONTACT_AUTH_PAGE).navigation();
                activity.finish();
                break;
            case AuthCodeLJ.BANK_INFO_CODE:
                ARouter.getInstance().build(RouterMapLJ.BANK_AUTH_PAGE).navigation();
                activity.finish();
                break;
            case AuthCodeLJ.ADDITIONAL_INFORMATION_CODE:
                ARouter.getInstance().build(RouterMapLJ.PHOTO_AUTH_PAGE).navigation();
                activity.finish();
                break;
            default:
                break;
        }

    }

    /**
     * 跳转下一个认证界面
     *
     * @param activity
     * @param router
     */
    public void jumpNextPager(final Activity activity, String router) {

        if (isLastPager(router)) {
            //已经是最后一步跳转认证状态页
            AcacheLJ.get(LJApplication.getContext()).remove(AcacheKeysLJ.AUTH);
            ARouter.getInstance().build(RouterMapLJ.STATUS_DISPLAY).
                    withInt(ConstantLJ.PAGE_TYPE, ConstantLJ.STATUS_TYPE_AUTH).navigation();
            activity.finish();
            return;
        }
        switch (router) {
            case RouterMapLJ.REAL_NAME_AUTH_PAGE:
                currentStep = AuthCodeLJ.IDENTITY_CODE;
                break;
            case RouterMapLJ.WORK_AUTH_PAGE:
                currentStep = AuthCodeLJ.COMPANY_CODE;
                break;
            case RouterMapLJ.MONEY_AUTH_PAGE:
                currentStep = AuthCodeLJ.BORROWING_NEED_CODE;
                break;
            case RouterMapLJ.PERSONAL_AUTH_PAGE:
                currentStep = AuthCodeLJ.PERSONAGE_INFO_CODE;
                break;
            case RouterMapLJ.CONTACT_AUTH_PAGE:
                currentStep = AuthCodeLJ.CONTACTS_CODE;
                break;
            case RouterMapLJ.BANK_AUTH_PAGE:
                currentStep = AuthCodeLJ.BANK_INFO_CODE;
                break;
            case RouterMapLJ.PHOTO_AUTH_PAGE:
                currentStep = AuthCodeLJ.ADDITIONAL_INFORMATION_CODE;
                break;
            default:
                break;
        }

        jumpPageAndFinish(activity, getNoAuthRouterList().get(getNoAuthRouterList().indexOf
                (currentStep) + 1));
    }

    public String routerMap2Code(String router) {
        String code = AuthCodeLJ.IDENTITY_CODE;
        switch (router) {
            case RouterMapLJ.REAL_NAME_AUTH_PAGE:
                code = AuthCodeLJ.IDENTITY_CODE;
                break;
            case RouterMapLJ.WORK_AUTH_PAGE:
                code = AuthCodeLJ.COMPANY_CODE;
                break;
            case RouterMapLJ.MONEY_AUTH_PAGE:
                code = AuthCodeLJ.BORROWING_NEED_CODE;
                break;
            case RouterMapLJ.PERSONAL_AUTH_PAGE:
                code = "personage_info_code";
                break;
            case RouterMapLJ.CONTACT_AUTH_PAGE:
                code = AuthCodeLJ.CONTACTS_CODE;
                break;
            case RouterMapLJ.BANK_AUTH_PAGE:
                code = AuthCodeLJ.BANK_INFO_CODE;
                break;
            case RouterMapLJ.PHOTO_AUTH_PAGE:
                code = AuthCodeLJ.ADDITIONAL_INFORMATION_CODE;
                break;
            default:
                break;
        }
        return code;
    }

    public void backRouter(String code) {
        switch (code) {
            case AuthCodeLJ.IDENTITY_CODE:
                ARouter.getInstance().build(RouterMapLJ.REAL_NAME_AUTH_PAGE).withBoolean(ConstantLJ
                        .IS_BACK, true).navigation();
                break;
            case AuthCodeLJ.COMPANY_CODE:
                ARouter.getInstance().build(RouterMapLJ.WORK_AUTH_PAGE).withBoolean(ConstantLJ
                        .IS_BACK, true).navigation();
                break;
            case AuthCodeLJ.BORROWING_NEED_CODE:
                ARouter.getInstance().build(RouterMapLJ.MONEY_AUTH_PAGE).withBoolean(ConstantLJ
                        .IS_BACK, true).navigation();
                break;
            case AuthCodeLJ.PERSONAGE_INFO_CODE:
                ARouter.getInstance().build(RouterMapLJ.PERSONAL_AUTH_PAGE).withBoolean(ConstantLJ
                        .IS_BACK, true).navigation();
                break;
            case AuthCodeLJ.CONTACTS_CODE:
                ARouter.getInstance().build(RouterMapLJ.CONTACT_AUTH_PAGE).withBoolean(ConstantLJ
                        .IS_BACK, true).navigation();
                break;
            case AuthCodeLJ.BANK_INFO_CODE:
                ARouter.getInstance().build(RouterMapLJ.BANK_AUTH_PAGE).withBoolean(ConstantLJ
                        .IS_BACK, true).navigation();
                break;
            case AuthCodeLJ.ADDITIONAL_INFORMATION_CODE:
                ARouter.getInstance().build(RouterMapLJ.PHOTO_AUTH_PAGE).withBoolean(ConstantLJ
                        .IS_BACK, true).navigation();
                break;
            default:
                break;
        }
    }

    public boolean isLastPager(String router) {
        if (getNoAuthRouterList().indexOf(routerMap2Code(router)) == getNoAuthRouterList().size()
                - 1) {
            return true;
        }
        return false;
    }

    public void jump2FirstNotAuthPager() {
        if (getNoAuthRouterList() != null && getNoAuthRouterList().size() >= 1) {
            jumpPage(getNoAuthRouterList().get(0));
        }
    }
}
